/**
 * @Author Admilk
 * @version 1.0.0
 */
import fs from "node:fs/promises";
import yaml from "yaml";
let file = "config/config/other.yaml";
export class master extends plugin {
    constructor() {
        super({
            name: "设置主人",
            dsc: "设置主人",
            event: "message",
            priority: -999999,
            rule: [
                { reg: "^#(设置|新增)主人$", fnc: "master", permission: "master" },
                { reg: "^#删除主人$", fnc: "removeMaster", permission: "master" },
            ],
        });
    }
    async read(file, key) {
        const doc = yaml.parseDocument(await fs.readFile(file, "utf8"));
        return doc.get(key) || [];
    }
    async write(file, doc) {
        await fs.writeFile(file, doc.toString(), "utf8");
    }
    async master() {
        let QQ = [
            ...new Set(this.e.message.filter((i) => i.type == "at").map((i) => i.qq)),
        ];
        if (!QQ.length) QQ.push(this.e.user_id);
        const doc = yaml.parseDocument(await fs.readFile(file, "utf8"));
        let docmaster = doc.get("master") || doc.createNode([]);
        let docmasterqq = doc.get("masterQQ") || doc.createNode([]);
        await Promise.all(
            QQ.map(async (qq) => {
                const isMaster =
                    docmasterqq.items.some((i) => i.value == qq) &&
                    docmaster.items.some((i) => i.value == `${this.e.self_id}:${qq}`);
                if (isMaster) {
                    await this.reply(`[${qq}] 已经是主人`, true);
                } else {
                    docmasterqq.items.push(doc.createNode(qq));
                    docmaster.items.push(doc.createNode(`${this.e.self_id}:${qq}`));
                    await this.reply(`[${qq}] 设置为主人成功`, true);
                }
            }),
        );
        doc.set("master", docmaster);
        doc.set("masterQQ", docmasterqq);
        await this.write(file, doc);
    }
    async removeMaster() {
        let QQ = [
            ...new Set(this.e.message.filter((i) => i.type == "at").map((i) => i.qq)),
        ];
        if (!QQ.length) QQ.push(this.e.user_id);
        const doc = yaml.parseDocument(await fs.readFile(file, "utf8"));
        let docmaster = doc.get("master") || [];
        let docmasterqq = doc.get("masterQQ") || [];
        await Promise.all(
            QQ.map(async (qq) => {
                const isMaster =
                    docmasterqq.items.some((i) => i.value == qq) &&
                    docmaster.items.some((i) => i.value == `${this.e.self_id}:${qq}`);
                if (!isMaster) {
                    await this.reply(`[${qq}] 不是主人`, true);
                } else {
                    docmasterqq.items = docmasterqq.items.filter((i) => i.value != qq);
                    docmaster.items = docmaster.items.filter(
                        (i) => i.value != `${this.e.self_id}:${qq}`,
                    );
                    await this.reply(`[${qq}] 已被删除为主人`, true);
                }
            }),
        );
        doc.set("master", docmaster);
        doc.set("masterQQ", docmasterqq);
        await this.write(file, doc);
    }
}
                                                                                                                    )